print("#####################################");
print("##Gameweb Functions Script Started###");
print("#####################################");
local Servername = "Gameweb - Server #1"
function FirstSpawn( ply )
	ply:EmitSound( "gameweb/welcome.mp3", 100, 100);
	ply:PrintMessage( HUD_PRINTTALK, "Welcome "..ply:GetName().."! Hope you enjoy playing with us!");
end
hook.Add( "PlayerInitialSpawn", "welcomeSound", FirstSpawn )

function CustomSound(ply, text)
	local Array = string.Explode( " ", text );
		
		if Array[1] == "/play" then
			ply:EmitSound( Array[2], 100, 100 );
			return false
		end
end
hook.Add( "PlayerSay", "playSound", CustomSound )

function Kick(ply, text)
	local plyexist = false
	local Array = string.Explode( " ", text );
	if Array[1] == "/kick" && IsGamewebAdmin(ply) == true && Array[2] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				WriteToLog( ply:GetName() .." kicked: ".. t:GetName() );
				table.remove( Array, 1)
				table.remove( Array, 1)
				gatekeeper.Drop(t:UserID(), string.Implode(" ", Array))
				ply:PrintMessage( HUD_PRINTTALK, "Kicked ".. t:GetName())
				plyexist = true		
			end
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "Kick", Kick )

function UserBan(ply, text)
	local plyexist = false
	local Array = string.Explode( " ", text );
	if Array[1] == "/ban" && IsGamewebAdmin(ply) == true && Array[2] && Array[3] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				Time = tonumber(Array[3])
				table.remove(Array,1)
				table.remove(Array,1)
				table.remove(Array,1)
				Reason = string.Implode(" ", Array)
				WriteToLog( ply:GetName() .." banned: ".. t:GetName() .. ", " .. t:SteamID().." couse of reason: ".. Reason );
				GamewebBan(ply,t,Time,Reason)
				plyexist = true
				ply:PrintMessage( HUD_PRINTTALK, "Banned ".. t:GetName())
			end
		end
		if plyexist == false then 
			if ply:IsValid() then
				ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
			end
		end
		return false
	end
end
hook.Add( "PlayerSay", "Ban", UserBan)

function UserUnban(ply, text)
	local Array = string.Explode( " ", text );
	if Array[1] == "/unban" && IsGamewebAdmin(ply) == true && Array[2] then
		WriteToLog( ply:GetName() .." Unbanned: ".. Array[2]);
		GamewebUnban(ply, Array[2])
		plyexist = true
		ply:PrintMessage( HUD_PRINTTALK, "Unbanned ".. Array[2])
		return false
	end
end
hook.Add( "PlayerSay", "UserUnban", UserUnban)

function HP(ply, text)
	local Array = string.Explode( " ", text );
	local plyexist = false
	if Array[1] == "/hp" && IsGamewebVip(ply) == true && Array[2] && Array[3] then
		for k , t in ipairs(player.GetAll()) do
				if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
					WriteToLog( ply:GetName() .." set " .. Array[2] .. " :" .. Array[3] .. " HP!" );
					plyexist = true
						if IsGamewebAdmin(ply) then
							t:SetHealth(tonumber(Array[3]))
						elseif tonumber(Array[3]) > 100 then
							t:SetHealth(100)
						else
							t:SetHealth(tonumber(Array[3]))
						end
					ply:PrintMessage( HUD_PRINTTALK, "Youve gave "..t:GetName().." HP!")
					t:PrintMessage( HUD_PRINTTALK, ply:GetName().." gave you HP!")
				end
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "HP", HP)

function Armor(ply, text)
	local Array = string.Explode( " ", text );
	local plyexist = false
	if Array[1] == "/armor" && IsGamewebVip(ply) == true && Array[2] && Array[3] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				WriteToLog( ply:GetName() .." set " .. Array[2] .. " :" .. Array[3] .. " Armor!" );
				plyexist = true
					if IsGamewebAdmin(ply) then
						t:SetArmor(Array[3])
					elseif tonumber(Array[3]) > 100 then
						t:SetArmor(100)
					else
						t:SetArmor(Array[3])
					end
				ply:PrintMessage( HUD_PRINTTALK, "Youve gave "..t:GetName().." Armor!")
				t:PrintMessage( HUD_PRINTTALK, ply:GetName().." gave you Armor!")
			end
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "Armor", Armor)

function Kill(ply, text)
	local plyexist = false
	local Array = string.Explode( " ", text );
	if Array[1] == "/kill" && IsGamewebAdmin(ply) == true && Array[2] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				WriteToLog( ply:GetName() .." killed " .. t:Nick() .. " via \"/kill\"" );
				t:Kill()
				plyexist = true
				ply:PrintMessage( HUD_PRINTTALK, "Killed ".. t:Nick())
			end
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "Kill", Kill)
function KD(ply, text)
	local Array = string.Explode( " ", text );
	if Array[1] == "/kd" then
		ply:PrintMessage( HUD_PRINTTALK, PlyKD(ply));			
		return false
	end
end
hook.Add( "PlayerSay", "KD", KD)

function Admin(ply, text)
	local Array = string.Explode( " ", text );
	local plyexist = false
	if Array[1] == "/addadmin" && IsGamewebOwner(ply) && Array[2] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				plyexist = true
				if IsGamewebAdmin(t) then
					ply:PrintMessage( HUD_PRINTTALK, t:Nick().." is the servers owner/admin!")	
				else
					WriteToLog( "Added Admin status to ".. t:Nick() );
					SetAdmin(t)
					ply:PrintMessage( HUD_PRINTTALK, "Gave admin status to ".. t:Nick())
					t:PrintMessage( HUD_PRINTTALK, ply:Nick().."gave you admin status!")
				end
			end	
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "Admin", Admin)

function RemoveAdmin(ply, text)
	local Array = string.Explode( " ", text );
	local plyexist = false
	if Array[1] == "/removeadmin" && IsGamewebOwner(ply) && Array[2] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				plyexist = true
				if IsGamewebAdmin(t) then
					ply:PrintMessage( HUD_PRINTTALK, t:Nick().." is the servers owner/admin!")	
				else
					WriteToLog( ply:Nick().." removed Admin status on ".. t:Nick() );
					RemoveAdmin(t)
					ply:PrintMessage( HUD_PRINTTALK, "Removed Admin status on ".. t:Nick())
					t:PrintMessage( HUD_PRINTTALK, ply:Nick().."removed your admin status!")					
				end
			end
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "RemoveAdmin", RemoveAdmin)
function God(ply, text)
	local plyexist = false
	local Array = string.Explode( " ", text );
	if Array[1] == "/god" && IsGamewebVip(ply) == true && Array[2] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				t:GodEnable();
				WriteToLog( ply:Nick().." god ".. t:Nick() );
				plyexist = true
				ply:PrintMessage( HUD_PRINTTALK, "Gave ".. t:Nick().." God Mode")
				t:PrintMessage( HUD_PRINTTALK, ply:Nick().." gave you God Mode!")
			end
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "God", God )
function UnGod(ply, text)
	local plyexist = false
	local Array = string.Explode( " ", text );
	if Array[1] == "/ungod" && IsGamewebAdmin(ply) == true && Array[2] then
			for k , t in ipairs(player.GetAll()) do
				if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
					t:GodDisable();
					WriteToLog( ply:Nick().." ungoded ".. t:Nick() );
					plyexist = true
						ply:PrintMessage( HUD_PRINTTALK, "Removed God Mode on ".. t:Nick())
						t:PrintMessage( HUD_PRINTTALK, ply:Nick().." removed God Mode from you!")
				end
			end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "UnGod", UnGod )

function Check(ply, text)
		if text == "/runcheck" && IsGamewebAdmin(ply) == true then
			for k , t in ipairs(player.GetAll()) do
				Reg(ply)	
			end
			return false
		end
end
hook.Add("PlayerSay","RunCheck",Check)

function AddVip(ply, text)
	local plyexist = false
	local Array = string.Explode( " ", text );	
	if Array[1] == "/addvip" && IsGamewebOwner(ply) && Array[2] then
			for k , t in ipairs(player.GetAll()) do
				if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
					if IsGamewebAdmin(t) then
						ply:PrintMessage( HUD_PRINTTALK, t:Nick().." is the server admin or owner!")	
					else
						WriteToLog( "Added VIP status to ".. t:Nick() );
						SetVip(t)
						ply:PrintMessage( HUD_PRINTTALK, "Added VIP status to ".. t:Nick())
						t:PrintMessage( HUD_PRINTTALK, "Your now a VIP!")
						plyexist = true
					end
				end	
			end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "AddVip", AddVip)
function Rcon(ply, text)
		local Array = string.Explode( " ", text );
		if Array[1] == "/rcon" && IsGamewebOwner(ply)  == true && Array[2] then
			RunConsoleCommand(Array[2], Array[3])
			return false
		end
end
hook.Add( "PlayerSay", "Rcon", Rcon)

function Give(ply, text)
	local plyexist = false
	local Array = string.Explode( " ", text );
	if Array[1] == "/give" && IsGamewebAdmin(ply) == true && Array[2] && Array[3] then
		for k , t in ipairs(player.GetAll()) do
			if string.find(string.lower(t:Nick()) , string.lower(Array[2])) then
				t:Give(Array[3])
				WriteToLog( ply:GetName() .." gave: " .. Array[2] .. " " .. Array[3] );
				plyexist = true	
			end
		end
		if plyexist == false then 
			ply:PrintMessage( HUD_PRINTTALK, "Player name wasnt valid!")
		end
		return false
	end
end
hook.Add( "PlayerSay", "Give", Give)

function Map(ply, text)
		local Array = string.Explode( " ", text );
		
		if Array[1] == "/changelevel" && IsGamewebAdmin(ply) == true && Array[2] then
			RunConsoleCommand("changelevel", Array[2])
			return false
		end
end
hook.Add( "PlayerSay", "Map", Map)